import Vue from 'vue'
import { getToken, removeToken, getAuthUserInfo, removeAuthUserInfo, removeInitUser } from "./auth";
import Variables from './variables'

var comsys = {
    install(Vue){
      /**
      *  储存localStorage
      */
      Vue.prototype.setlocalStorage = (storeName, storeData) => {
        var storeStr = JSON.stringify(storeData);
        return window.localStorage.setItem(storeName, storeStr);
      },
      /**
      *  获取localStorage
      */
      Vue.prototype.getlocalStorage = (storeName) => {
        var storeName = localStorage.getItem(storeName);
        return JSON.parse(storeName)
      },
      /**
      *  储存sessionStorage
      */
      Vue.prototype.setsessionStorage = (storeName, storeData) => {
        var storeStr = JSON.stringify(storeData);
        return window.sessionStorage.setItem(storeName, storeStr);
      },
      /**
      *  获取sessionStorage
      */
      Vue.prototype.getsessionStorage = (storeName) => {
        var storeName = sessionStorage.getItem(storeName);
        return JSON.parse(storeName)
      },
      /**
      *  获取url后面的参数
      */
      Vue.prototype.getUrlParams = (name) => {
        if(process.browser){
          var _reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"),
              _regNext = window.location.search.substr(1).match(_reg);
          if (_regNext != null) return decodeURI(_regNext[2]) || '';
          else return '';
        }
      },
      // 跳转外面链接
      Vue.prototype.routeToOut = (url, type) => {
        if(type && type == 'new'){
          window.open(url)
        }else{
          window.location.href = url
        }
      },
      /**
      *  站内跳转
        url 跳转的url
        type 跳转类型 自动拼接mall前缀
        type: new 新页面打开
        type: current 当前页面打开
      */
      Vue.prototype.routeToPath = (url, to, type) => {
        var toStr = '/mallplus';
        if(type && type == 'new'){
          window.open(toStr + url);
        }else{
          window.location.href = (toStr + url)
        }
      },
      /**
      *  返回用户信息
      */
      Vue.prototype.getUserInfo = () => {
        if(getAuthUserInfo()){
          return JSON.parse(getAuthUserInfo())
        }
      },
          /**
    *  格式化价格
    */
    Vue.prototype.filterPrice = (price) => {
      if(price){
        return '￥<span class="price-number">'+price.split('.')[0] +'</span>.'+price.split('.')[1];
      }
    },
  /**
  *  友善提示
  */
  Vue.prototype.ElNote = (tips) => {
    ELEMENT.Notification({
      title: '提示',
      message: tips,
      duration: 1000
    });
  },

  // 跳转去老平台
  Vue.prototype.goWebsite = (mallType, id, type, domainName) => {
    if(mallType == 1){
      if(type == 'p'){
        window.open('/mallplus/product/'+id);
      }else if (type == 'c') {
        window.open('/mallplus/factory/'+id);
      }
    }else if(mallType == 2){
      if(type == 'p'){
        window.open('http://www.youboy.com/s'+id+'.html')
      }else if (type == 'c' && domainName) {
        window.open('http://'+domainName+'.b2b.youboy.com')
      }
    }
  },
  // 下载文档
  Vue.prototype.downFile = (imgsrc, name) => {
    var blob = new Blob(imgsrc, {type: "text/plain;charset=utf-8"});
    FileSaver.saveAs(blob, name);
    // let objectUrl = URL.createObjectURL(blob);
    // let link = document.createElement("a");
    // let fname = `我的文档`; //下载文件的名字
    // link.href = url;
    // link.setAttribute("download", fname);
    // document.body.appendChild(link);
    // window.open(url)
  },
  /**
  *  刷新当前页面
  */
  Vue.prototype.reloadPage = (time) => {
    setTimeout(() => {
      router.go(0)
    }, time)
  },
  /**
  *  判断协议订单的文件类型
  */
  Vue.prototype.judgeFileType = (file) => {
    var file = file.substr(file.lastIndexOf(".")).toLowerCase();
    if(file === '.doc'){
      return 'isDoc'
    }else if(file === '.jpg' || file === '.png' || file === '.jpeg') {
      return 'isImage'
    }else if(file === '.pdf') {
      return 'isPdf'
    }
  },
  Vue.prototype.loginOut = () => {
    removeToken()
    removeAuthUserInfo()
    removeInitUser()

    setTimeout(()=>{

      window.location.href = '/mallplus/login'
    }, 5)
  },
  /**
   *  获取token
   */
  Vue.prototype.getAuthToken = () => {
    return getToken()
  },
  /**
  *  格式化时间
  */
  Vue.prototype.fmtDate = (date, type) => {
    if(date == null){
      return null
    }

    function add0(m) {
        return m < 10 ? '0' + m: m
    }
    var time = new Date(date);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    if(type && type == 'ymd'){
      return y + '-' + add0(m) + '-' + add0(d)
    }else if(type && type == 'ymd2'){
      return y + '年' + add0(m) + '月' + add0(d) + '日'
    }else{
      return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    }
  },
  Vue.prototype.routeToGoods = (id) => {
    // wx.miniProgram.getEnv(function(res) {
    //   // 小程序端
    //   if(res.miniprogram){
      //   wx.miniProgram.navigateTo({url: '/packageIndex/pages/shopProductDetails?productId='+id})
      // }else{
        window.open('https://www.youboy.com/mall/product/detail?id=' + id);
      // }
    // })

  },
  /**
  *  操作成功之后
  */
  Vue.prototype.actionsAfter = (tips, type, url) => {
    ELEMENT.Message({
      type: type,
      message: '提示：'+tips
    });
    setTimeout(()=>{
      return url?router.go(-1):''
    }, 1000)
  },
  /**
  *  打印
  */
  Vue.prototype.log = (obj, type) => {
    type = type || "log";
    const log = JSON.parse(JSON.stringify(obj));
    return console[type](log)
  }
}
}
Vue.use(comsys);
